import { useState, useEffect, useRef } from 'react'
import { useLocation, useNavigate } from "react-router-dom"
import axios from '../../http'

export default function index() {
    let { state } = useLocation()
    let navigate = useNavigate()
    // console.log(state)
    let [user, setuser] = useState<any>(state || { email: "", pwd: "", verif: "" })
    let change1 = (e: Event, type: string) => {
        user[type] = (e.target as any).value
        setuser(JSON.parse(JSON.stringify(user)))
    }
    let login = async () => {
        let res = await axios.post("/egg/login", user)
        // let res = { data: { code: 2003, info: "邮箱不存在" } }
        console.log(res)
        if (res.data.code == 2005) {
            navigate("/jack/home")
        }
        else {
            alert(res.data.info)
        }

    }


    async function loadverif() {
        let res = await axios.get("/egg/verif")
        console.log(res.data);
        (box.current as HTMLElement).innerHTML = res.data.info
    }
    let box = useRef({})
    useEffect(() => {

        loadverif()
    }, [])

    return (
        <div>
            <h1>登录页面</h1>
            email<input type="text" value={user.email} onInput={(e: any) => change1(e, "email")} /> <br />
            pwd<input type="text" value={user.pwd} onInput={(e: any) => change1(e, "pwd")} /> <br />
            验证码<input type="text" value={user.verif} onInput={(e: any) => change1(e, "verif")} /><br />
            <span onClick={loadverif} ref={box as any}></span>
            <button onClick={login}>登录</button>
        </div>
    )
}
